package utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.ygk.bluepath.core.BApp;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;

public class UIOperateTools {
	private static BApp app = BApp.getInstance();
	static TelephonyManager telphony = (TelephonyManager) app
			.getSystemService(Context.TELEPHONY_SERVICE);

	private static int sdk = app.getSharedPreUtil().getSdk();

	public static void setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			// pre-condition
			return;
		}

		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();

		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
	}

	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	public static void setGridViewHeight(GridView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			// pre-condition
			return;
		}

		int totalHeight = 8;
		for (int i = 0; i < listAdapter.getCount(); i++) {

			if (i % 2 == 0) {
				View listItem = listAdapter.getView(i, null, listView);
				listItem.measure(0, 0);

				totalHeight += listItem.getMeasuredHeight() + 5;
			}

		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();

		// params.height = totalHeight + (listView.getVerticalSpacing() *
		// (listAdapter.getCount() /2));
		params.height = totalHeight;
		listView.setLayoutParams(params);
	}

	@SuppressWarnings("deprecation")
	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	public static void setBackgroundDrawable(View view, Drawable drawable) {

		if (sdk == 0) {
			sdk = android.os.Build.VERSION.SDK_INT;
			app.getSharedPreUtil().setSdk(sdk);
		}

		if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {

			view.setBackgroundDrawable(drawable);

		} else {

			view.setBackground(drawable);
		}

	}

	public static int stoi(String intstr) {
		int i = 0;
		if (intstr.equals("")) {
			return i;

		} else {
			Integer integer;
			integer = Integer.valueOf(intstr);
			return integer.intValue();
		}

	}

	public static void savePic(Bitmap bm, String fileName, String load) {
		File dirFile = new File(load);
		if (!dirFile.exists()) {
			dirFile.mkdirs();
		}
		File myCaptureFile = new File(load + fileName);
		try {
			FileOutputStream bos = new FileOutputStream(myCaptureFile);
			// bm=Tools.toRoundCorner(bm);
			bm.compress(Bitmap.CompressFormat.PNG, 100, bos);
			bos.flush();
			bos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static Bitmap bitmapResize(Bitmap bitmap, float w, float h) {
		Matrix matrix = new Matrix();
		matrix.postScale(w, h); // 长和宽放大缩小的比例
		Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
				bitmap.getHeight(), matrix, true);
		return resizeBmp;
	}

	public static void deleteDir(String PATH) {
		File dir = new File(PATH);
		if (dir == null || !dir.exists() || !dir.isDirectory())
			return;

		for (File file : dir.listFiles()) {
			if (file.isFile())
				file.delete(); //
			else if (file.isDirectory())

				if (PATH.endsWith("/")) {
					deleteDir(PATH + file.getName()); //
				} else {
					deleteDir(PATH + "/" + file.getName()); //
				}

		}
		dir.delete();// 鍒犻櫎鐩綍鏈韩
	}

	/**
	 * @return 
	 * @Description: TODO
	 * @param    
	 * @return void    
	 * @throws 
	 */
	public static String[] getFullName(String strPath) {
		// TODO Auto-generated method stub
		AssetManager am=app.getAssets();
		String[] fullName = null; 
		try {
			String[]  name=am.list(strPath);
			fullName=new String[name.length];
			
			for (int i = 0; i < name.length; i++) {
				System.out.println("name:"+name[i]);
				fullName[i]="assets://"+strPath+"/"+name[i];
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		for (int i = 0; i < fullName.length; i++) {
			System.out.println("fullName:"+fullName[i]);
		}
		
		return fullName;
	}

}